//
//  SYNotiViewController.m
//  ChatDemo-UI2.0
//
//  Created by zz on 16/5/26.
//  Copyright © 2016年 zz. All rights reserved.
//

#import "SYNotiViewController.h"


#import "CustomAlert.h"
#import "Reachability.h"
#import "SYParNotiTableViewCell.h"
#import "DropDownMenu2.h"
#import "EmptyStateView.h"

@interface SYNotiViewController ()<UITableViewDelegate,UITableViewDataSource,DropDownMenu2Delegate,UIAlertViewDelegate,FinishHomeWorkDelegate,UIGestureRecognizerDelegate,yunDouAlertDelegate,reloadDataDelegate>

@property(nonatomic,strong) NSMutableArray *classArr; //班级通知数
@property(nonatomic,strong) NSMutableArray *pubsArr; //我发出的
@property(nonatomic,strong) NSMutableArray *classes; //用户所有班级数
@property(nonatomic,strong) NSMutableArray *classeids; //用户所有班级数id
@property(nonatomic,strong) NSMutableArray *editClasses;//编辑数组

@property(nonatomic,strong) Reachability *re;


@end

@implementation SYNotiViewController{
    
    UIButton *classWork;
    UIButton *myPub;
    UIView *lineViw;
    UIScrollView *_scrollView;
    UITableView *_classTable; //班级通知
    UITableView *_pubTable; //我发出的
    int lastType;//登录身份
    
    UIBarButtonItem *selRightBarbtn; //筛选
    UIBarButtonItem *editRightBarbtn; //编辑
    UIBarButtonItem *addRightBtn; //老师身份点击留通知
    
    DropDownMenu2 *drop;
    
    NSString *classid;
    
    int homeworkPageNum; //班级通知的pageno
    int myPubHomework; //我发布的通知的pageno
    
    NSString *studentId; //学生id
    
    UIView *noworkView; //没有班级通知的view
    UIView *noPubView; //没有我发出的view
    
    BOOL pubRefresh;
    
    BOOL claRefresh;
    
    NSInteger parMinInfoid;//用于家长数据的最小infoid
    NSInteger pubMinInfoid;//老师身份发布数据的最小infoid
    
    UIButton *editBtn;//编辑按钮
    BOOL isEdit;//Yes编辑状态
    
    UIButton *allmarkBtn;//全部标记按钮
    UIButton *allDeleteBtn;//删除
    UIView *btnLine;
    
    UILabel *footerLabel;
    EmptyStateView *emptyView;//空状态
    EmptyStateView *temptyView;//空状态
    EmptyStateView *pubemptyView;//空状态
    
    EmptyStateView *noWifiView;//无网络状态
    EmptyStateView *cnoWifiView;//无网络状态
    int  wifiMark;// 1班级数据 2 我发出的
   
   NSInteger schoolManager;//>0是学校管理员
   NSString * schoolID;
    
}

-(NSMutableArray *)editClasses
{
    if (_editClasses == nil) {
        
        _editClasses = [NSMutableArray array];
    }
    
    return _editClasses;
}

-(NSMutableArray *)classArr{
    
    if (_classArr == nil) {
        _classArr = [NSMutableArray array];
    }
    return _classArr;
}

-(NSMutableArray *)pubsArr{
    
    if (_pubsArr == nil) {
        _pubsArr = [NSMutableArray array];
    }
    return _pubsArr;
}

-(void)initBtns
{
    allmarkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    allmarkBtn.frame=CGRectMake(0, SCREENHEIGH-48.5, SCREENWIDTH/2, 48.5);
    [allmarkBtn setBackgroundColor:[UIColor whiteColor]];
    [allmarkBtn setTitle:@"全部已读" forState:UIControlStateNormal];
    [allmarkBtn setTitleColor:[UIColor colorWithHexString:@"#39adff"] forState:UIControlStateNormal];
    [allmarkBtn addTarget:self action:@selector(allMarkAction:) forControlEvents:UIControlEventTouchUpInside];
    allmarkBtn.hidden=YES;
    [self.view addSubview:allmarkBtn];
    
    allDeleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    allDeleteBtn.frame=CGRectMake(SCREENWIDTH/2, SCREENHEIGH-48.5, SCREENWIDTH/2, 48.5);
    [allDeleteBtn setBackgroundColor:[UIColor whiteColor]];
    [allDeleteBtn setTitle:@"删除" forState:UIControlStateNormal];
    [allDeleteBtn setTitleColor:[UIColor colorWithHexString:@"#dddddd"] forState:UIControlStateNormal];
    [allDeleteBtn addTarget:self action:@selector(allDeleteAction:) forControlEvents:UIControlEventTouchUpInside];
    allDeleteBtn.hidden= YES;
    allDeleteBtn.userInteractionEnabled=NO;
    [self.view addSubview:allDeleteBtn];
    
    btnLine = [[UIView alloc]initWithFrame:CGRectMake(0, SCREENHEIGH-49, SCREENWIDTH, 0.5)];
    btnLine.backgroundColor=RGBCOLOR(199, 199, 204);
    btnLine.hidden=YES;
    [self.view addSubview:btnLine];
}

-(void)allMarkAction:(UIButton *)sender
{
    if ([sender.titleLabel.text isEqualToString:@"全部已读"]) {
        
        
        UIAlertView *alert =[[UIAlertView alloc]initWithTitle:nil message:@"确定将班级通知全部标记已读？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.tag=111;
        [alert show];
        
        
    }else
    {
        UIAlertView *alert =[[UIAlertView alloc]initWithTitle:nil message:@"确定将已选的班级通知标记已读？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.tag=112;
        [alert show];
        
       
    }
}
-(void)allDeleteAction:(UIButton *)sender
{
    UIAlertView *alert =[[UIAlertView alloc]initWithTitle:nil message:@"删除通知后将无法找回，确定删除已选的班级通知？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.tag=113;
    [alert show];
    
   
}
-(void)initArray{
    
    _classArr = [NSMutableArray array];
    _pubsArr = [NSMutableArray array];
    _classes = [NSMutableArray array];
    _classeids = [NSMutableArray array];
}

-(void)initDrop{

    [_classes insertObject:@"全部" atIndex:0];

}

-(void)initClassid{
    
    classid = @"0"; //默认是0;
    
}

#pragma mark -UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    

    if (buttonIndex == 0) {
        
        if (alertView.tag==111 || alertView.tag==112 || alertView.tag==113) {
            
            return;
        }else
        {
            [self.navigationController popViewControllerAnimated:YES];
        }
        
    }else{
        
//        UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
//        
//        if (alertView.tag == 102) {  //创建或者关联学生
//            
//            StudentViewController *stuVC = [mainSB instantiateViewControllerWithIdentifier:@"StudentViewController"];
//            
//            [self.navigationController pushViewController:stuVC animated:YES];
//            
//        }else if(alertView.tag == 101){  //创建或者关联班级
//            
//            UIViewController *imfomationVC;
//            
//            if (lastType == 1){
//                
//                imfomationVC = [mainSB instantiateViewControllerWithIdentifier:@"ClassTeamViewController"];
//            }
//            
//            [self.navigationController pushViewController:imfomationVC animated:YES];
//            
//        }else
           
      if (alertView.tag==111)
        {
            NSMutableDictionary *param = [NSMutableDictionary dictionary];
            
            [param setObject:@"api_info_allread" forKey:@"method"];
            
            if (USERID) {
                
                [param setObject:USERID forKey:@"userid"];
            }
            
            [param setObject:@"0" forKey:@"studentid"];

            [param setObject:@"2" forKey:@"type"];
            
            [Utils get:nil parameters:param success:^(NSInteger recode, id jsonObject) {
                
                [self.navBar.rightButton2 setTitle:@"编辑" forState:UIControlStateNormal];
                
                self->allmarkBtn.hidden = YES;
                self->allDeleteBtn.hidden = YES;
                self->btnLine.hidden = YES;
                
                [UIView animateWithDuration:0.3 animations:^{
                    
                    self->_classTable.frame = CGRectMake(-33, SafeAreaTopHeight, SCREENWIDTH+33, SCREENHEIGH-SafeAreaTopHeight);
                    self->footerLabel.frame= CGRectMake(33, 0.4, VIEW_WIDTH+33, 59.6);
                }];
                
                self->_classTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
                    
                    [self parentHomeworkData];
                }];
                
                for (int i=0; i<self.classArr.count; i++) {
                    
                    homework *model = [self.classArr objectAtIndex:i];
                    
                    if ([model.isread isEqualToString:@"0"]) {
                        
                        model.isread = @"1";
                    }
                    
                    [self.classArr replaceObjectAtIndex:i withObject:model];
                }
                
                [SYClassesTool setAllNotificationAndHomeworkIsread:0];
                
                [self->_classTable reloadData];
                
                self->isEdit=NO;
                
                
            } failure:^(NSInteger recode, NSError *error) {
                
                
            }];

        }else if (alertView.tag==112)
        {
            NSMutableDictionary *param = [NSMutableDictionary dictionary];
            
            [param setObject:@"api_infos_read" forKey:@"method"];
            
            if (USERID) {
                
                [param setObject:USERID forKey:@"userid"];
            }
            
            [param setObject:@"0" forKey:@"studentid"];
            
            if (self.editClasses.count>0) {
                
                NSMutableArray *idArrs = [NSMutableArray array];
                
                for (int i=0; i<self.editClasses.count; i++) {
                    
                    homework *model = [self.editClasses objectAtIndex:i];
                    
                    [idArrs addObject:model.infoid];
                }
                
                NSString *infoIdStr =[idArrs componentsJoinedByString:@","];
                
                [param setObject:infoIdStr forKey:@"infoids"];
            }
            
            [Utils get:nil parameters:param success:^(NSInteger recode, id jsonObject) {
                
                [self.navBar.rightButton2 setTitle:@"编辑" forState:UIControlStateNormal];
                
                self->allmarkBtn.hidden = YES;
                self->allDeleteBtn.hidden = YES;
                self->btnLine.hidden = YES;
                
                [UIView animateWithDuration:0.3 animations:^{
                    
                    self->_classTable.frame = CGRectMake(-33, SafeAreaTopHeight, SCREENWIDTH+33, SCREENHEIGH-SafeAreaTopHeight);
                    
                    self->footerLabel.frame= CGRectMake(33, 0.4, VIEW_WIDTH+33, 59.6);
                }];
                
                self->_classTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
                    
                    [self parentHomeworkData];
                }];
                
                for (int i=0; i< self.editClasses.count; i++) {
                    
                    homework *model=[self.editClasses objectAtIndex:i];
                    
                    for (int j=0; j<self.classArr.count; j++) {
                        
                        homework * model2=[self.classArr objectAtIndex:j];
                        
                        if ([model.infoid isEqualToString:model2.infoid]) {
                            
                            model2.isEdit = NO;
                            
                            model2.isread = @"1";
                            
                            [self.classArr replaceObjectAtIndex:j withObject:model2];
                            
                            break;
                        }
                    }
                }
                
                [SYClassesTool setNotificationAndHomeworkAsRead:self.editClasses Notificationtype:0];
                
                [self.editClasses removeAllObjects];
                
                [self->_classTable reloadData];
                
                self->isEdit=NO;
                
            } failure:^(NSInteger recode, NSError *error) {
                
                
            }];

        }else if (alertView.tag==113)
        {
            NSMutableDictionary *param = [NSMutableDictionary dictionary];
            
            [param setObject:@"api_infos_delete" forKey:@"method"];
            
            if (USERID) {
                
                [param setObject:USERID forKey:@"userid"];
            }
            
                [param setObject:@"0" forKey:@"studentid"];
            
            if (self.editClasses.count>0) {
                
                NSMutableArray *idArrs = [NSMutableArray array];
                
                for (int i=0; i<self.editClasses.count; i++) {
                    
                    homework *model = [self.editClasses objectAtIndex:i];
                    
                    [idArrs addObject:model.infoid];
                }
                
                NSString *infoIdStr =[idArrs componentsJoinedByString:@","];
                
                [param setObject:infoIdStr forKey:@"infoids"];
            }
            
            [Utils get:nil parameters:param success:^(NSInteger recode, id jsonObject) {
                
                [self.navBar.rightButton2 setTitle:@"编辑" forState:UIControlStateNormal];
                
                self->allmarkBtn.hidden = YES;
                self->allDeleteBtn.hidden = YES;
                self->btnLine.hidden = YES;
                
                [UIView animateWithDuration:0.3 animations:^{
                    
                    self->_classTable.frame = CGRectMake(-33, SafeAreaTopHeight, SCREENWIDTH+33, SCREENHEIGH-SafeAreaTopHeight);
                    
                    self->footerLabel.frame= CGRectMake(33, 0.4, VIEW_WIDTH, 59.6);
                }];
                
                self->_classTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
                    
                    [self parentHomeworkData];
                }];
                
                for (int i=0; i< self.editClasses.count; i++) {
                    
                    homework *model=[self.editClasses objectAtIndex:i];
                    
                    for (int j=0; j<self.classArr.count; j++) {
                        
                        homework * model2=[self.classArr objectAtIndex:j];
                        
                        if ([model.infoid isEqualToString:model2.infoid]) {
                            
                            [self.classArr removeObject:model2];
                            
                            break;
                        }
                    }
                }
                
                if(self.classArr.count == 0)
                {
                    self.navBar.rightButton2 = nil;
                }
                
                
                
                [SYClassesTool deleteNotificationAndHomework:self.editClasses Notificationtype:0];
                
                [self.editClasses removeAllObjects];
                
                [self->_classTable reloadData];
                
                self->isEdit=NO;
                
            } failure:^(NSInteger recode, NSError *error) {
                
                
            }];

        }
    }
}

#pragma mark -加载用户的所有班级
-(void)loadClassData{
    
    lastType = [SYAccountTool ShareAccount].account.lasttype;
    schoolManager = [SYAccountTool ShareAccount].account.schoolManage;
    schoolID = [SYAccountTool ShareAccount].account.scid;
   
    if (lastType == 1) {  //老师
        
        [self allClassData];
        
    }else if (lastType == 2){ //家长
        
       self.navBar.rightButton1.hidden = YES;
       self.navBar.rightButton.hidden = YES;
       self.navBar.rightImg.hidden = YES;
       self.navBar.rightButton2.hidden = NO;
        
//        studentId = [[NSUserDefaults standardUserDefaults] objectForKey:@"studentID"];
//        
//        if (!studentId) { //没有学生，需要提示
//            
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请先创建/关联学生" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
//            alert.tag = 102;
//            [alert show];
//            
//            return;
//            
//        }else{
//            
//            self.navigationItem.rightBarButtonItem = self->editRightBarbtn;
//            
//        }
    }
}

-(void)allClassData{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setObject:@"api_classone_search" forKey:@"method"];
    [param setObject:USERID forKey:@"userid"];
    [param setObject:@"1" forKey:@"pageno"];
    [param setObject:@"1000000000" forKey:@"pagesize"];
    
    [param setObject:@(lastType) forKey:@"type"];
    [param setObject:@"1" forKey:@"state"];
    
    [Utils getAllClass:nil parameters:param success:^(NSInteger recode, id jsonObject) {
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        if (arr.count == 0) {  //没有班级
            
            NSString *message;
            
            if (self->lastType == 1) {
                
                message = @"请先创建/加入班级";
            }else{
                message = @"请先加入班级";
            }
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
            alert.tag = 101;
            [alert show];
            
        }else if(arr.count > 0){
            
            [self->_classeids addObjectsFromArray:arr];
            
            for (NSDictionary *dic in arr) {
                
                NSString *className = [dic objectForKey:@"classename"];
                
                [self->_classes addObject:className];
            }
            
           self.navBar.rightImg.hidden = NO;
           self.navBar.rightButton.hidden = NO;
           self.navBar.rightButton1.hidden = YES;
           self.navBar.rightButton2.hidden = YES;
            
            
            
            [self initDrop];
        }
        
    } failure:^(NSInteger recode, NSError *error) {
        
        
        
    }];
    
}

-(void)initPageNum{
    
    homeworkPageNum = 2;
}

#pragma mark -没有通知的情况
-(void)initNoworkView{
    
    noworkView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGH - 64)];
    noworkView.backgroundColor = [UIColor clearColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn sizeToFit];
    btn.frame = CGRectMake(0, 0, SCREENWIDTH, noworkView.frame.size.height);
    
    int type = [SYAccountTool ShareAccount].account.lasttype;
    
    //老师身份的时候是暂无
    if (type == 1) {

        [btn setImage:[UIImage imageNamed:@"no_mg_"] forState:UIControlStateNormal];
    }else if (type == 2){
        
        [btn setImage:[UIImage imageNamed:@"no_message_pic_"] forState:UIControlStateNormal];
    }
    
    btn.enabled = NO;
    
    [noworkView addSubview:btn];
    
    noworkView.hidden = YES;
    
}

-(void)configureNav{
    
    self.navBar.nav_titleLabel.text = @"通知";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalName:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = backItem;
    
   [self.navBar.rightButton2 setTitle:@"编辑" forState:UIControlStateNormal];
   [self.navBar.rightButton2 setTitleColor:RGBACOLOR(57, 173, 255, 1) forState:UIControlStateNormal];
   [self.navBar.rightButton2 addTarget:self action:@selector(eidtClassList:) forControlEvents:UIControlEventTouchUpInside];
   self.navBar.rightButton2.titleLabel.font=[UIFont systemFontOfSize:16];
   self.navBar.rightButton2.imageEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
   
   
   [self.navBar.rightButton1 setTitle:@"筛选" forState:UIControlStateNormal];
   [self.navBar.rightButton1 setTitleColor:RGBACOLOR(57, 173, 255, 1) forState:UIControlStateNormal];
   [self.navBar.rightButton1 addTarget:self action:@selector(selClass:) forControlEvents:UIControlEventTouchUpInside];
   self.navBar.rightButton1.titleLabel.font=[UIFont systemFontOfSize:16];
   self.navBar.rightButton1.imageEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
   
   
   self.navBar.rightImg.image = [UIImage imageWithOriginalName:@"add_"];
   [self.navBar.rightButton addTarget:self action:@selector(rightbarbtnClick) forControlEvents:UIControlEventTouchUpInside];

    
    noWifiView = [[EmptyStateView alloc] initNoWifiWithFrame:CGRectMake(0, (SCREENHEIGH-64-175)/2, SCREENWIDTH, 175) andBackImage:@"noWifi" andBottomStr:@"网络出错啦,点击重新加载"];
    noWifiView.userInteractionEnabled= YES;
    noWifiView.delegate = self;
    [self.view addSubview:noWifiView];
    
    cnoWifiView = [[EmptyStateView alloc] initNoWifiWithFrame:CGRectMake(0, (SCREENHEIGH-64-175)/2, SCREENWIDTH, 175) andBackImage:@"noWifi" andBottomStr:@"网络出错啦,点击重新加载"];
    cnoWifiView.userInteractionEnabled= YES;
    cnoWifiView.delegate = self;
    [self.view addSubview:cnoWifiView];


}

#pragma mark -导航条右侧按钮的点击事件
-(void)back{
    
    if (lastType==2) {
        
        self->_classTable.frame = CGRectMake(SafeAreaTopHeight, 0, SCREENWIDTH, SCREENHEIGH-SafeAreaTopHeight);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)rightbarbtnClick{
    
    if (lastType == 1) {
        
        PushNotiViewController *pstVC = [[PushNotiViewController alloc] init];
        pstVC.listArray = self.classeids;
        pstVC.delegate=self;
        [self.navigationController pushViewController:pstVC animated:YES];
    }
}

//选择班级
-(void)selClass:(UIButton *)sender{

    drop = [DropDownMenu2 shareDropDownMenu];
    drop.delegate = self;
    drop.flag=YES;
    drop.backViewFlag = 4;
    [drop setDropDownMenuParameterWithcellContents:_classes andWidth:125];
    [drop hideDropDown:sender];
    
    UIWindow *window=KeyWindow;
    [window addSubview:drop];

}

- (void)eidtClassList:(UIButton *)sender{
    
    isEdit = !isEdit;
    
    if (isEdit == YES) {
        
        
        [self.navBar.rightButton2 setTitle:@"取消" forState:UIControlStateNormal];

        allmarkBtn.hidden = NO;
        [self->allmarkBtn setTitle:@"全部已读" forState:UIControlStateNormal];
        [self->allmarkBtn setTitleColor:[UIColor colorWithHexString:@"#39adff"] forState:UIControlStateNormal];
        
        allDeleteBtn.hidden = NO;
        [self->allDeleteBtn setTitleColor:[UIColor colorWithHexString:@"#dddddd"] forState:UIControlStateNormal];
        self->allDeleteBtn.userInteractionEnabled = NO;

        btnLine.hidden = NO;
        
        [UIView animateWithDuration:0.3 animations:^{
            
            self->_classTable.frame = CGRectMake(0, SafeAreaTopHeight, SCREENWIDTH, SCREENHEIGH-SafeAreaTopHeight-49);
            
            self->footerLabel.frame= CGRectMake(-33/2, 0.4, VIEW_WIDTH+33, 59.6);
        }];
        
        
        self->_classTable.mj_header = nil;

    }else
    {
        [self.navBar.rightButton2 setTitle:@"编辑" forState:UIControlStateNormal];
        
        allmarkBtn.hidden = YES;
        allDeleteBtn.hidden = YES;
        btnLine.hidden = YES;
    
        
        [self.editClasses removeAllObjects];
        
        for (int i=0; i<self.classArr.count; i++) {
            
            homework *model = [self.classArr objectAtIndex:i];
            
            if (model.isEdit) {
                
                model.isEdit = NO;
            }
            
            [self.classArr replaceObjectAtIndex:i withObject:model];
        }
        
        [self->_classTable reloadData];
        
        [UIView animateWithDuration:0.3 animations:^{
            
            self->_classTable.frame = CGRectMake(-33, SafeAreaTopHeight, SCREENWIDTH+33, SCREENHEIGH-SafeAreaTopHeight);
        }];
        
        _classTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [self parentHomeworkData];
        }];

        
    }
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [self initArray];
    
    [self initNoworkView];  //初始化没有通知情况的view
   
    [self initClassid]; //classid
    
    [self initPageNum];
    
    [self loadClassData]; //加载班级
    
    [self configureNav];
    
    [self configureUI];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(CustomAlertShow:) name:@"CustomAlertShow" object:nil];
   
}

-(void)CustomAlertShow:(NSNotification *)object
{
    NSDictionary *dic=object.userInfo;
    NSString *num = [dic objectForKey:@"num"] ;
    [self showYunDouAlert:@"发通知" andNum:[num intValue]];
        
}
-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark -网络状态发生改变
-(void)networkStateChange{

    // 1.检测wifi状态
    Reachability *wifi = [Reachability reachabilityForLocalWiFi];
    
    // 2.检测手机是否能上网络(WIFI\3G\2.5G)
    Reachability *conn = [Reachability reachabilityForInternetConnection];
    
    // 3.判断网络状态
    if ([wifi currentReachabilityStatus] != NotReachable) { // 有wifi
        
        [self showHint:@"网络连接已恢复"];
        [self.classArr removeAllObjects];
        homeworkPageNum = 1;
        
        [self.pubsArr removeAllObjects];
        myPubHomework = 1;

    } else if ([conn currentReachabilityStatus] != NotReachable) { // 没有使用wifi, 使用手机自带网络进行上网
        NSLog(@"使用手机自带网络进行上网");
        [self showHint:@"网络连接已恢复"];
        [self.classArr removeAllObjects];
        homeworkPageNum = 1;
        
        [self.pubsArr removeAllObjects];
        myPubHomework = 1;

    } else { // 没有网络
        
        [self showHint:@"无网络连接，请查看您的网络"];
    }

}

-(void)configureUI{
    
    lastType = [SYAccountTool ShareAccount].account.lasttype;
    if (lastType == 1) { //老师身份
        
        [self teacherView];
        
    }else{  //家长身份
        
        [self parentView];
    }
}

#pragma mark -家长身份与老师身份的布局
//老师身份
-(void)teacherView{
    
    //班级通知
    classWork = [UIButton buttonWithType:UIButtonTypeCustom];
    [classWork setTitle:@"班级通知" forState:UIControlStateNormal];
    [classWork setTitleColor:[UIColor colorWithRed:0.0 green:0.7059 blue:1.0 alpha:1.0] forState:UIControlStateSelected];
    [classWork setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    classWork.frame = CGRectMake(0, SafeAreaTopHeight, SCREENWIDTH/2, 55);
    classWork.titleLabel.font = [UIFont systemFontOfSize:17];

    classWork.selected = NO;
    [classWork addTarget:self action:@selector(classWorkClick:) forControlEvents:UIControlEventTouchUpInside];
    classWork.tag = 1;
    [self.view addSubview:classWork];
    
    //我发出的
    myPub = [UIButton buttonWithType:UIButtonTypeCustom];
    [myPub setTitle:@"我发出的" forState:UIControlStateNormal];
    [myPub setTitleColor:[UIColor colorWithRed:0.0 green:0.7059 blue:1.0 alpha:1.0] forState:UIControlStateSelected];
    [myPub setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    myPub.frame = CGRectMake(SCREENWIDTH/2,SafeAreaTopHeight, SCREENWIDTH/2, 55);
    myPub.titleLabel.font = [UIFont systemFontOfSize:17];
    [myPub addTarget:self action:@selector(myPubClick:) forControlEvents:UIControlEventTouchUpInside];
    myPub.tag = 2;
    myPub.selected = YES;
    [self.view addSubview:myPub];
    
    //线
    UIView *bgLineView = [[UIView alloc] init];
    bgLineView.backgroundColor = [UIColor lightGrayColor];
    bgLineView.frame = CGRectMake(0,  SafeAreaTopHeight+56.5, SCREENWIDTH, 0.5);
    [self.view addSubview:bgLineView];
    
    lineViw = [[UIView alloc] init];
    lineViw.backgroundColor = [UIColor colorWithRed:0.0 green:0.7059 blue:1.0 alpha:1.0];
    CGFloat w = SCREENWIDTH/2;
    lineViw.frame = CGRectMake(30, 54 + SafeAreaTopHeight,w - 60 , 3);
    [self.view addSubview:lineViw];
    
    //scrollView
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight + 55 + 2, SCREENWIDTH, SCREENHEIGH - SafeAreaTopHeight - 57)];
    _scrollView.contentSize = CGSizeMake(SCREENWIDTH * 2, SCREENHEIGH - 121);
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    
    //tableView
    _classTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGH - SafeAreaTopHeight - 57  ) style:UITableViewStylePlain];
    _classTable.delegate = self;
    _classTable.dataSource = self;
    _classTable.tableFooterView = [[UIView alloc] init];
    _classTable.separatorStyle=UITableViewCellSeparatorStyleNone;
    _classTable.showsVerticalScrollIndicator=NO;

    _classTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self loadClassHomeworkWithType:0];
    
    }];
    
    [_classTable.mj_header beginRefreshing];
    
    _classTable.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self loadMoreClassHomeworkWithType:0];
    }];
    
    [_scrollView addSubview:_classTable];
    
    emptyView = [[EmptyStateView alloc] initWithFrame:CGRectMake((SCREENWIDTH-120)/2+33, (SCREENHEIGH-110)/2-70, 120, 110) andBackImage:@"notification_empty" andBottomStr:@"暂无班级通知"];
    [_classTable addSubview:emptyView];
    
    temptyView = [[EmptyStateView alloc] initWithFrame:CGRectMake((SCREENWIDTH-120)/2, (SCREENHEIGH-110)/2-70, 120, 110) andBackImage:@"notification_empty" andBottomStr:@"暂无班级通知"];
    [_classTable addSubview:temptyView];
    
    _pubTable = [[UITableView alloc] initWithFrame:CGRectMake(SCREENWIDTH, 0, SCREENWIDTH, SCREENHEIGH - SafeAreaTopHeight - 57) style:UITableViewStylePlain];
    _pubTable.delegate = self;
    _pubTable.dataSource = self;
    _pubTable.tableFooterView = [[UIView alloc] init];
    _pubTable.separatorStyle=UITableViewCellSeparatorStyleNone;
    _pubTable.showsVerticalScrollIndicator=NO;
    _pubTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self loadMyPubHomeworkWithType:0];
    }];
    
    
    
    _pubTable.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self loadMoreMyPubHomeworkWithType:0];
    }];
    
    //获取数据库的shuju
    
//    [_pubTable.mj_header beginRefreshing];
    [self loadMyPubHomeworkWithType:1];
    
    //设置偏移量，先进了时候显示的是我发出的通知
    CGPoint point = _scrollView.contentOffset;
    point.x = SCREENWIDTH;
    _scrollView.contentOffset = point;
    
    [_scrollView addSubview:_pubTable];
    
    pubemptyView = [[EmptyStateView alloc] initWithFrame:CGRectMake(0, (SCREENHEIGH-110)/2-70, SCREENWIDTH, 110) andBackImage:@"notification_empty" andBottomStr:@"您还没有发过通知"];
    [_pubTable addSubview:pubemptyView];

}

-(void)parentView{
    
    _classTable = [[UITableView alloc] initWithFrame:CGRectMake(-33, SafeAreaTopHeight, SCREENWIDTH+33, SCREENHEIGH-SafeAreaTopHeight ) style:UITableViewStylePlain];
    _classTable.delegate = self;
    _classTable.dataSource = self;
    _classTable.tableFooterView = [[UIView alloc] init];
    _classTable.separatorStyle=UITableViewCellSeparatorStyleNone;
    _classTable.showsVerticalScrollIndicator=NO;
        
    _classTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self parentHomeworkData];
    }];

    
    
    [_classTable.mj_header beginRefreshing];
    
    _classTable.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self parentMoreHomeworkData];
    }];

    
    [self.view addSubview:_classTable];
    
    emptyView = [[EmptyStateView alloc] initWithFrame:CGRectMake((SCREENWIDTH-120)/2+33, (SCREENHEIGH-110)/2-70, 120, 110) andBackImage:@"notification_empty" andBottomStr:@"暂无班级通知"];
    [_classTable addSubview:emptyView];

    //获取家长的缓存数据
    [self getDbParentData];
    
//    [self.view addSubview:noworkView];
    
    [self initBtns];
}

#pragma mark -UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (scrollView == _scrollView) {
        
        CGFloat x = lineViw.frame.origin.x;
        
        x = 30 + scrollView.contentOffset.x / 2.0;
        
        lineViw.frame = CGRectMake(x, lineViw.frame.origin.y, lineViw.frame.size.width, lineViw.frame.size.height);
    }
    
    
//    [drop hidden];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if (scrollView == _scrollView) {
        
        if (scrollView.contentOffset.x == SCREENWIDTH) {
//            [self myPubClick:nil];
        }else if (scrollView.contentOffset.x < SCREENWIDTH){
           // [self classWorkClick:nil];
        }
    }
    
}

#pragma mark -按钮的点击事件
// 班级通知
-(void)classWorkClick:(UIButton *)sender{
    
    [cnoWifiView hideEmptyImage];
    _scrollView.hidden = NO;
//    [self loadClassHomework];
    
    myPub.selected = NO;
    classWork.selected = YES;
    __block CGRect frame = lineViw.frame;
    
    CGFloat w = SCREENWIDTH / 2;
    
    __block CGFloat x = frame.origin.x;
    
    if (x > w) {
        
        [UIView animateWithDuration:0.3 animations:^{
            x = x - w;
            frame = CGRectMake(x, frame.origin.y, frame.size.width, frame.size.height);
            self->lineViw.frame = frame;
            self->_scrollView.contentOffset = CGPointMake(0, 0);
        }];
        
    }
    
    
   //更换右侧的按钮显示筛选
   self.navBar.rightButton.hidden = YES;
   self.navBar.rightButton2.hidden = YES;
   self.navBar.rightImg.hidden = YES;
   
   
   self.navBar.rightButton1.hidden = NO;
}

//我的通知
-(void)myPubClick:(UIButton *)sender{
    
    [noWifiView hideEmptyImage];
    _scrollView.hidden = NO;
//    [self loadMyPubHomework];
    
    
    myPub.selected = YES;
    classWork.selected = NO;
    
    __block CGRect frame = lineViw.frame;
    
    CGFloat w = SCREENWIDTH / 2;
    
    __block CGFloat x = frame.origin.x;
    
    if (x < w) {
        
        [UIView animateWithDuration:0.3 animations:^{
            x = x + w;
            frame = CGRectMake(x, frame.origin.y, frame.size.width, frame.size.height);
            self->lineViw.frame = frame;
            self->_scrollView.contentOffset = CGPointMake(SCREENWIDTH, 0);
        }];
        
    }
    
    
   //显示加号
   self.navBar.rightButton2.hidden = YES;
   self.navBar.rightButton1.hidden = YES;
   self.navBar.rightButton.hidden = NO;
   self.navBar.rightImg.hidden = NO;
}

#pragma mark -UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView == _classTable) {
        return _classArr.count;
    }else{
        return _pubsArr.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *scellid;
    
    homework *model;
    if (tableView == _classTable) {  //班级通知
        
        if (lastType==1) {
            
            scellid = @"cellid";
            model = [_classArr objectAtIndex:indexPath.row];
            
            SYNotiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:scellid];
            
            if (cell == nil) {
                cell = [[SYNotiTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:scellid];
            }
            
            UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 113.5, SCREENWIDTH, 0.5)];
            line.backgroundColor=RGBACOLOR(199, 199, 204, 1);
            [cell.contentView addSubview:line];
            [cell updateCellWithModel:model];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;

        }else
        {
            scellid = @"cellid";
            model = [_classArr objectAtIndex:indexPath.row];
            
            SYParNotiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:scellid];
            
            cell.selectBtn.userInteractionEnabled = NO;
            if (cell == nil) {
                cell = [[SYParNotiTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:scellid];
            }
            
            if (model.isEdit) {
                
                [cell.selectBtn setImage:[UIImage imageNamed:@"new_parents_noticehw_list_press_"] forState:UIControlStateNormal];
            }else
            {
                [cell.selectBtn setImage:[UIImage imageNamed:@"new_parents_noticehw_list_nor_"] forState:UIControlStateNormal];
            }

            
            UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 97.5, SCREENWIDTH+33, 0.5)];
            line.backgroundColor=RGBACOLOR(199, 199, 204, 1);
            [cell.contentView addSubview:line];
            [cell updateCellWithModel:model];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;

        }
        
    }else{
        
        scellid = @"mycellid";
        model = [_pubsArr objectAtIndex:indexPath.row];
        SYNotiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:scellid];
        
        if (cell == nil) {
            cell = [[SYNotiTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:scellid];
        }
        UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 113.5, SCREENWIDTH, 0.5)];
        line.backgroundColor=RGBACOLOR(199, 199, 204, 1);
        [cell.contentView addSubview:line];
        model.ismyPub = 1;
        [cell updateCellWithModel:model];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    //判断网络环境，
//    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
//    [manager startMonitoring];
//    
//    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
//        
//        if (status == 0) {
//            
//            [Utils alertViewWithMessage:@"请在有网络连接的情况下查看详情"];
//            return;
//        }
    
        if (tableView == self->_classTable) {
//            if (self->isOpen==YES) {
//                self->isOpen=NO;
//                [self->drop hidden];
//            }else
//            {
                if (self->lastType==1) {
                    homework *model = [self->_classArr objectAtIndex:indexPath.row];
                    
                    DetailHvkViewController *vc = [[DetailHvkViewController alloc] init];
                    vc.model = model;
                    vc.delegate = self;
                    vc.index = indexPath.row;
                    [self.navigationController pushViewController:vc animated:YES];
                    
                }else if (self->lastType==2&&self->isEdit==NO)
                {
                    homework *model = [self->_classArr objectAtIndex:indexPath.row];
                    
                    DetailHvkViewController *vc = [[DetailHvkViewController alloc] init];
                    vc.model = model;
                    vc.delegate = self;
                    vc.index = indexPath.row;
                    [self.navigationController pushViewController:vc animated:YES];
                }else if (self->lastType==2&&self->isEdit==YES)
                {
                    homework *model=[self->_classArr objectAtIndex:indexPath.row];
                    
                    model.isEdit = !model.isEdit;
                    
                    SYParNotiTableViewCell *cell =[self->_classTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
                    
                    if (model.isEdit) {
                        
                        [cell.selectBtn setImage:[UIImage imageNamed:@"new_parents_noticehw_list_press_"] forState:UIControlStateNormal];
                        
                        [self.editClasses addObject:model];
                        
                    }else
                    {
                        [cell.selectBtn setImage:[UIImage imageNamed:@"new_parents_noticehw_list_nor_"] forState:UIControlStateNormal];
                        [self.editClasses removeObject:model];
                    }
                  
                    [self->_classArr replaceObjectAtIndex:indexPath.row withObject:model];
                    
                    if (self.editClasses.count>0) {
                        
                         [self->allmarkBtn setTitle:@"标记已读" forState:UIControlStateNormal];
                         [self->allDeleteBtn setTitleColor:[UIColor colorWithHexString:@"#39adff"] forState:UIControlStateNormal];
                         self->allDeleteBtn.userInteractionEnabled = YES;
                    }else
                    {
                        [self->allmarkBtn setTitle:@"全部已读" forState:UIControlStateNormal];
                        [self->allDeleteBtn setTitleColor:[UIColor colorWithHexString:@"#39adff"] forState:UIControlStateNormal];
                        
                        [self->allDeleteBtn setTitleColor:[UIColor colorWithHexString:@"#dddddd"] forState:UIControlStateNormal];
                        self->allDeleteBtn.userInteractionEnabled = NO;
                    }
                    
                }
                
                
//            }
            
        }else if (tableView == self->_pubTable){
            
            homework *model = [self->_pubsArr objectAtIndex:indexPath.row];
            
            NoticecountViewController *vc = [[NoticecountViewController alloc] init];
            vc.model = model;
            
            [self.navigationController pushViewController:vc animated:YES];
        }

//    }];
    
}

#pragma mark -老师身份的通知数据
//老师身份 班级的通知
-(void)loadClassHomeworkWithType:(int)type{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setObject:@"api_info_list" forKey:@"method"];
    [param setObject:classid forKey:@"classid"];
    [param setObject:USERID forKey:@"userid"];
    [param setObject:@"2" forKey:@"type"];
    [param setObject:@"0" forKey:@"studentid"];
    [param setObject:@10 forKey:@"pagesize"];
   if (schoolManager > 0) {
      
      [param setObject:schoolID forKey:@"scid"];
   }
    if (type == 1) [Utils showHUD];
    
    [Utils noIndiGet:nil parameters:param success:^(NSInteger recode, id jsonObject) {
        if (type == 1) [Utils hideHUD];
        [self->_classArr removeAllObjects];
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        if (arr.count == 0) {

            [self->temptyView showEmptyImage];
            
            [self ->_classTable reloadData];
            [self ->_classTable.mj_header endRefreshing];
            self ->_classTable.tableFooterView = [[UIView alloc] init];
            
            return;
        }else if (arr.count == 10){
            
            self ->claRefresh = YES;
        }else{
            self ->claRefresh = NO;
        }

        [self->temptyView hideEmptyImage];
        
        for (NSDictionary *dic in arr) {
            
            homework *model = [homework mj_objectWithKeyValues:dic];
            [self->_classArr addObject:model];
        }
        
        self->homeworkPageNum = 2;
        
        [self ->_classTable reloadData];
        [self -> _classTable.mj_header endRefreshing];
        
        
    } failure:^(NSInteger recode, NSError *error) {
        if (type == 1) [Utils hideHUD];
        [self->_classTable.mj_header endRefreshing];
        if (error.code == -1009) {
            
            self->wifiMark = 1;
            self->_scrollView.hidden = YES;
            [self ->noWifiView showEmptyImage];
        }
    }];
}

//老师身份 加载更多班级通知
-(void)loadMoreClassHomeworkWithType:(int)type{
    
    if (type == 1) [Utils hideHUD];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:@"api_info_list" forKey:@"method"];
    [param setObject:classid forKey:@"classid"];
    [param setObject:USERID forKey:@"userid"];
    [param setObject:@"2" forKey:@"type"];
    [param setObject:@(homeworkPageNum) forKey:@"pageno"];
    [param setObject:@10 forKey:@"pagesize"];
    [param setObject:@"0" forKey:@"studentid"];
   if (schoolManager > 0) {
      
      [param setObject:schoolID forKey:@"scid"];
   }
    [Utils noIndiGet:nil parameters:param success:^(NSInteger recode, id jsonObject) {
        if (type == 1) [Utils hideHUD];
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        if (arr.count == 10) {
            self->homeworkPageNum ++;
            self ->claRefresh = YES;
        }else{
            self ->claRefresh = NO;
        }
        
        for (NSDictionary *dic in arr) {
            
            homework *model = [homework mj_objectWithKeyValues:dic];
            [self->_classArr addObject:model];
        }
        
        [self ->_classTable reloadData];
        [self -> _classTable.mj_footer endRefreshing];
        
        
        
        
    } failure:^(NSInteger recode, NSError *error) {
        
        if (type == 1) [Utils hideHUD];
        
        if (error.code == -1009) {
            
            self->wifiMark = 1;
            self->_scrollView.hidden = YES;
            [self ->noWifiView showEmptyImage];
        }
    }];
    
}

//我发布的通知
-(void)loadMyPubHomeworkWithType:(int)type{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    if (type == 1) {
        
        [Utils showHUD];
    }
    
    [param setObject:@"api_info_list" forKey:@"method"];
    [param setObject:USERID forKey:@"createuserid"];
    [param setObject:USERID forKey:@"userid"];
    [param setObject:@"2" forKey:@"type"];
    [param setObject:@"0" forKey:@"studentid"];
    [param setObject:@10 forKey:@"pagesize"];
   if (schoolManager > 0) {
      
      [param setObject:schoolID forKey:@"scid"];
   }
    [Utils noIndiGet:nil parameters:param success:^(NSInteger recode, id jsonObject) {
        
        if (type == 1) {
            
            [Utils hideHUD];
        }
        
        [self->_pubsArr removeAllObjects];
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        if(arr.count == 0){
            
//            [self noPubView];
            [self->pubemptyView showEmptyImage];
            [self ->_pubTable reloadData];
            [self ->_pubTable.mj_header endRefreshing];
            
            return;
        }else if(arr.count == 10){
            
            self ->pubRefresh = YES;
        }else{
            
            self ->pubRefresh = NO;
        }
        
//        self->noPubView.hidden = YES;
        [self->pubemptyView hideEmptyImage];
        
        int i = 0;
        for (NSDictionary *dic in arr) {
            
            i++;
            
            homework *model = [homework mj_objectWithKeyValues:dic];
            [self->_pubsArr addObject:model];
            
            if (i == arr.count) {
                
                self ->pubMinInfoid = [model.infoid integerValue];
            }
            
            [SYClassesTool saveTeacherNotificationWithDict:dic withType:@"teacher"];
        }
        
        
        self->myPubHomework = 2;
        
        [self ->_pubTable reloadData];
        [self ->_pubTable.mj_header endRefreshing];
                
    } failure:^(NSInteger recode, NSError *error) {
        
        if (type == 1) {
            
            [Utils hideHUD];
        }
        
        [self getmyPubDBdata];
        
        if (error.code == -1009 && self.pubsArr.count == 0) {
            
            self->wifiMark = 2;
            self->_scrollView.hidden = YES;
            [self ->cnoWifiView showEmptyImage];
        }else
        {
            [self->_pubTable.mj_header endRefreshing];
        }
    }];
}


//上啦加载更多的发布班级数据
-(void)loadMoreMyPubHomeworkWithType:(int)type{
    
    if (type == 1) {
        
        [Utils showHUD];
    }
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setObject:@"api_info_list" forKey:@"method"];
    [param setObject:USERID forKey:@"createuserid"];
    [param setObject:USERID forKey:@"userid"];
    [param setObject:@"2" forKey:@"type"];
    [param setObject:@(myPubHomework) forKey:@"pageno"];
    [param setObject:@10 forKey:@"pagesize"];
    [param setObject:@"0" forKey:@"studentid"];
   if (schoolManager > 0) {
      
      [param setObject:schoolID forKey:@"scid"];
   }
    
    [Utils noIndiGet:nil parameters:param success:^(NSInteger recode, id jsonObject) {
        
        if (type == 1) [Utils hideHUD];
            
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        if (arr.count == 10) {
            self->myPubHomework++;
            self ->pubRefresh = YES;
        }else{
            self ->pubRefresh = NO;
        }
        
        int i = 0;
        for (NSDictionary *dic in arr) {
            
            homework *model = [homework mj_objectWithKeyValues:dic];
            [self->_pubsArr addObject:model];
            
            if (i == arr.count) {
                
                self ->pubMinInfoid = [model.infoid integerValue];
            }
            
            [SYClassesTool saveTeacherNotificationWithDict:dic withType:@"teacher"];
        }
        
        [self ->_pubTable reloadData];
        [self ->_pubTable.mj_footer endRefreshing];
        

        
    } failure:^(NSInteger recode, NSError *error) {
        
        if (type == 1) [Utils hideHUD];
        
        if (error.code == -1009 && self.pubsArr.count == 0) {
            
            self->wifiMark = 2;
            self->_scrollView.hidden = YES;
            [self ->cnoWifiView showEmptyImage];
        }else
        {
            NSString *str = error.description;
            
            NSArray *arr = [str componentsSeparatedByString:@" "];
            if (arr.count > 3) {
                
                NSString *code = arr[2];
                if ([code isEqualToString:@"Code=-1009"]) { //断网的情况
                    
                    //从数据库加载更多我发布的数据
                    [self loadMorePubDataFromDBWithInfoid:self->parMinInfoid];
                }
            }
        }

    }];
    
}


#pragma mark -从数据库获取数据
-(void)getmyPubDBdata{
    
    self.pubsArr = [SYClassesTool getTeacherNotificationWithType:@"teacher"];
    
    if (self.pubsArr.count < 10) {
        
        pubRefresh = NO;
        
    }else
    {
        pubRefresh = YES;
        
    }
    
    [_pubTable reloadData];
    
    for (int i = 0; i < self.pubsArr.count; i++) {
        
        homework *model = self.pubsArr.lastObject;
        pubMinInfoid = [model.infoid integerValue];
    }
}

-(void)getDbParentData{
    
    self.classArr = [SYClassesTool getTeacherNotificationWithType:@"parents"];
    
    if (self.classArr.count < 10) {
        
        claRefresh = NO ;
    }else
    {
        claRefresh = YES;
    }
    
    
    for (int i = 0; i < self.classArr.count; i++) {
        
        homework *model = self.classArr.lastObject;
        parMinInfoid = [model.infoid integerValue];
    }
    [_classTable reloadData];
}

-(void)loadMoreParDataFromDBWithInfoid:(NSInteger)infoid{
    
    NSMutableArray *arr = [SYClassesTool getMoreDataWithinfoid:parMinInfoid andTypeStr:@"parents"];
    [_classArr addObjectsFromArray:arr];
    
    if (arr.count < 10) {
        
        claRefresh = NO;
        [_classTable.mj_footer endRefreshing];
    }
    
    [_classTable reloadData];
    
    for (int i = 0; i < arr.count; i++) {
        
        homework *model = arr.lastObject;
        parMinInfoid = [model.infoid integerValue];
    }
}

-(void)loadMorePubDataFromDBWithInfoid:(NSInteger)infoid{
    
    NSMutableArray *arr = [SYClassesTool getMoreDataWithinfoid:pubMinInfoid andTypeStr:@"teacher"];
    
    
    [_pubsArr addObjectsFromArray:arr];
    
    if (arr.count < 10) {
        
        self ->pubRefresh = NO;
        [self->_pubTable.mj_footer endRefreshing];

    }
    
    [_pubTable reloadData];
    
    for (int i = 0; i < self.pubsArr.count; i++) {
        
        homework *model = self.pubsArr.lastObject;
        pubMinInfoid = [model.infoid integerValue];
    }

}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){
        
        if (tableView == _pubTable) {
            
            if (pubRefresh == NO) {
                
                UIView *view =[[ UIView alloc] initWithFrame:CGRectMake(0, _pubTable.contentSize.height, VIEW_WIDTH, 60)];
                UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 0.4)];
                lineView.backgroundColor = RGBACOLOR(222, 223, 229, 1);
//                [view addSubview:lineView];
                
                UILabel *label=[[UILabel alloc]init];
                label.frame= CGRectMake(0, 0.4, VIEW_WIDTH, 59.6);

                label.text = @"没有更多内容了";
                label.font = [UIFont boldSystemFontOfSize:14];
                label.textAlignment = NSTextAlignmentCenter;
                [view addSubview:label];
                label.textColor = [UIColor colorWithHexString:@"979797"];
                _pubTable.tableFooterView = view;
                _pubTable.mj_footer = nil;
            }else if (pubRefresh == YES){
                
                _pubTable.tableFooterView = nil;
                _pubTable.mj_footer = _pubTable.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                    
                    [self loadMoreMyPubHomeworkWithType:0];
                }];
            }
        }else if (tableView == _classTable){
            
            if (claRefresh == NO) {
                
                UIView *view =[[ UIView alloc] init ];
                if (lastType == 1) {
                    
                    view.frame= CGRectMake(0, 0.4, VIEW_WIDTH, 59.6);
                }else
                {
                    if (isEdit) {
                    
                        view.frame= CGRectMake(-33, 0.4, VIEW_WIDTH+33, 59.6);
                        
                    }else
                    {
                        view.frame= CGRectMake(0, 0.4, VIEW_WIDTH, 59.6);
                    }
                    
                }

                view.frame=CGRectMake(0, _classTable.contentSize.height, VIEW_WIDTH, 60);
                UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 0.4)];
                lineView.backgroundColor = RGBACOLOR(222, 223, 229, 1);
//                [view addSubview:lineView];
                
                footerLabel=[[UILabel alloc]init];
                
                if (lastType == 1) {
                
                    footerLabel.frame= CGRectMake(0, 0.4, VIEW_WIDTH, 59.6);
                }else
                {
                    if (isEdit) {
                        
                        footerLabel.frame= CGRectMake(-33, 0.4, VIEW_WIDTH+33, 59.6);
                        
                    }else
                    {
                        footerLabel.frame= CGRectMake(33, 0.4, VIEW_WIDTH, 59.6);
                    }
                    
                }
                footerLabel.text = @"没有更多内容了";
                footerLabel.textAlignment = NSTextAlignmentCenter;
                footerLabel.font = [UIFont boldSystemFontOfSize:14];
                footerLabel.textColor = [UIColor colorWithHexString:@"979797"];
                [view addSubview:footerLabel];
                _classTable.tableFooterView = view;
                _classTable.mj_footer = nil;
            }else if (claRefresh == YES){
                
                _classTable.tableFooterView = nil;
                _classTable.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                    
                    int type = [SYAccountTool ShareAccount].account.lasttype;
                    
                    if (type == 1) {
                        
                        [self loadMoreClassHomeworkWithType:0];
                        
                    }else if (type == 2){
                        
                        [self parentMoreHomeworkData];
                    }

                }];
            }

        }
        
        //end of loading //for example [activityIndicator stopAnimating]; }}
    }
}

#pragma mark -加载家长身份班级通知
-(void)parentHomeworkData{
    
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:@"api_info_liststudent" forKey:@"method"];
    [dict setObject:@"10" forKey:@"pagesize"];
    [dict setObject:@"1" forKey:@"pageno"];
    [dict setObject:@"0" forKey:@"classid"];
    [dict setObject:@"2" forKey:@"type"];
    [dict setObject:@"0" forKey:@"studentid"];
    
    [dict setObject:USERID forKey:@"userid"];
    
    [Utils noIndiGet:nil parameters:dict success:^(NSInteger recode, id jsonObject) {
        
        [self->_classArr removeAllObjects];
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        if (arr.count == 0) { //没有通知
            
//            [self initNohomeWorkView];
            
            [self->emptyView showEmptyImage];
            [self ->_classTable reloadData];
            [self ->_classTable.mj_header endRefreshing];
            self ->_classTable.tableFooterView = [[UIView alloc] init];
            self.navBar.rightButton2.hidden = nil;
            return;
        }
        
       //显示编辑按钮
       self.navBar.rightButton1.hidden = YES;
       self.navBar.rightButton.hidden = YES;
       self.navBar.rightImg.hidden = YES;
       self.navBar.rightButton2.hidden = NO;
       
        [self->emptyView hideEmptyImage];
        
        if (arr.count == 10) {
            self->homeworkPageNum = 2;
            self ->claRefresh = YES;
        }else{
            self ->claRefresh = NO;
        }
        
        int i = 0;
        for (NSDictionary *dic in arr) {
            
            i++;
            homework *model = [homework mj_objectWithKeyValues:dic];
            [self->_classArr addObject:model];
            
            //给parmininfoid赋值
            if (i == arr.count) {
                
                self->parMinInfoid = [model.infoid integerValue];
            }
        }
        
        [self ->_classTable reloadData];
        
        [self->_classTable.mj_header endRefreshing];
        
        for (NSDictionary *dic in arr) {
            

            [SYClassesTool saveTeacherNotificationWithDict:dic withType:@"parents"];
            
        }
        
    } failure:^(NSInteger recode, NSError *error) {
        
       
        
        if (error.code == -1009 && self.classArr.count == 0) {
            self->wifiMark = 3;
            self->_classTable.hidden = YES;
            [self ->noWifiView showEmptyImage];
        }else
        {
            [self->_classTable.mj_header endRefreshing];
        }
        
    }];
    
}

//家长身份加载更多数据
-(void)parentMoreHomeworkData{
    
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:@"api_info_liststudent" forKey:@"method"];
    [dict setObject:@(homeworkPageNum) forKey:@"pageno"];
    [dict setObject:@"0" forKey:@"classid"];
    [dict setObject:@"2" forKey:@"type"];
    [dict setObject:@10 forKey:@"pagesize"];
//    if (studentId) {
//    [dict setObject:studentId forKey:@"studentid"];
//    }
    [dict setObject:@"0" forKey:@"studentid"];
    
    [dict setObject:USERID forKey:@"userid"];
    
    
    [Utils noIndiGet:nil parameters:dict success:^(NSInteger recode, id jsonObject) {
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        if (arr.count == 10) {
            
            self->homeworkPageNum++;
            self ->claRefresh = YES;
        }else{
            self ->claRefresh = NO;
        }

        int i = 0;
        for (NSDictionary *dic in arr) {
            
            homework *model = [homework mj_objectWithKeyValues:dic];
            [self->_classArr addObject:model];
            
            //给parmininfoid赋值
            if (i == arr.count) {
                
                self->parMinInfoid = [model.infoid integerValue];
            }
            
            [SYClassesTool saveTeacherNotificationWithDict:dic withType:@"parents"];
        }
        
        [self ->_classTable reloadData];
        
        [self->_classTable.mj_footer endRefreshing];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        
        if (error.code == -1009 && self.classArr.count == 0) {
            self->wifiMark = 3;
            self->_classTable.hidden = YES;
            [self ->noWifiView showEmptyImage];
        }else
        {
            NSString *str = error.description;
            
            NSArray *arr = [str componentsSeparatedByString:@" "];
            if (arr.count > 3) {
                
                NSString *code = arr[2];
                if ([code isEqualToString:@"Code=-1009"]) { //断网的情况
                    
                    [self showHint:@"网络连接已中断"];
                    
                    //从数据库加载更多数据
                    [self loadMoreParDataFromDBWithInfoid:self->parMinInfoid];
                }
            }

        }
    }];
    
}

-(void)reloadCurrentData
{
   
    
    if (wifiMark == 1) {

        [cnoWifiView hideEmptyImage];
        _scrollView.hidden= NO;
        [self loadClassHomeworkWithType:0];

    }else if (wifiMark == 2)
    {
        [noWifiView hideEmptyImage];
        _scrollView.hidden = NO;
        [self loadMyPubHomeworkWithType:0];
    }else if (wifiMark == 3)
    {
        [noWifiView hideEmptyImage];
        _classTable.hidden = NO;
        [self parentHomeworkData];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //if (tableView == _classTable) {
    
    //homework *model = [_classArr objectAtIndex:indexPath.row];
    
    //return model.cellHeight;
    //}else{
    
    if (lastType==1) {
        
         return 114;
    }else
    {
         return 98;
    }
   
    //}
    
}

-(void)niDropDownDelegateMethod:(DropDownMenu2 *)sender{
    
    
    _classTable.hidden = NO;
    
    NSDictionary *dic = [_classeids objectAtIndex:sender.senderTag - 1];
    
    NSString *classidStr = [dic objectForKey:@"classid"];
    
    switch (sender.senderTag) {
        case 0:  //全选
            classid = 0;
            [_classTable.mj_header beginRefreshing];
            break;
            
        default:
            classid = classidStr;
            [_classTable.mj_header beginRefreshing];
            break;
    }
    
}



#pragma mark -没有通知的情况
-(void)initNohomeWorkView{
    
    noworkView.hidden = NO;
    
    //[self.view bringSubviewToFront:noworkView];
}

-(void)noPubView{
    
    noPubView.hidden = NO;
}

#pragma mark -通知已读，刷新模型
-(void)updateModel:(homework *)model andIndex:(NSInteger)index{
    
    [_classArr replaceObjectAtIndex:index withObject:model];
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    
    [_classTable reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}


-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    if ([SYAccountTool ShareAccount].needRefresh == 1) {
        
        [_pubTable.mj_header beginRefreshing];
        [_classTable.mj_header beginRefreshing];
    }
}

-(void)viewWillDisappear:(BOOL)animated{
    
    [SYAccountTool ShareAccount].needRefresh = 0;
    
//    [drop hidden];
    
    [super viewWillDisappear:animated];
}
////点击手势的DELEGATE回调方法
//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//    if ([touch.view isKindOfClass:[UITableView class]]) {
//        //判断是否出现（即尺寸不为0时）不加此判断可能会出现，随便点击就出菜单
//        if(drop.frame.size.height>0){
//            [drop hidden];
//            isOpen=NO;
//        }
//        return YES;
//    }
//    
//    return  NO;
//}
-(void)showYunDouAlert:(NSString *)string andNum:(int)num
{
    
    CustomAlert *alert=[CustomAlert ShareCustomAlert];
    [alert showAlertTaskString:string andNum:num];
}

//-(void)Choosecell:(NSInteger)index
//{
//    homework *model=[_classArr objectAtIndex:index];
//   
//    model.isEdit = !model.isEdit;
//    
//    SYParNotiTableViewCell *cell =[_classTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
//    if (model.isEdit) {
//        
//        [cell.selectBtn setImage:[UIImage imageNamed:@"new_parents_noticehw_list_press_"] forState:UIControlStateNormal];
//    }else
//    {
//        [cell.selectBtn setImage:[UIImage imageNamed:@"new_parents_noticehw_list_nor_"] forState:UIControlStateNormal];
//    }
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
